import React from 'react';
import {Toast} from 'antd-mobile'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import {actions} from './reducers'

import MyRouter from 'route/route';

class App extends React.Component {
    constructor(props) {
        super(props);
            this.openNotification = this.openNotification.bind(this);
    }
    openNotification(type, message) {
        let that = this,
                _time = 1
        if (type == 'fail') {
            _time = 3
        } else if (type=='success') {
            _time = 1
        }
        Toast[type](message, _time)
        that.props.clear_msg();
    };
    render() {
        this.props.notification && this.props.notification.content ?
            this.openNotification(this.props.notification.type, this.props.notification.content):
            null
        return (
            <React.Fragment>
                <MyRouter/>
            </React.Fragment>
        )
    }
}

function mapStateToProps(state) {
    return {
        notification: state.globalState.msg,
    }
}

function mapDispatchToProps(dispatch) {
    return  {
        clear_msg: bindActionCreators(actions.clear_msg, dispatch),
    }
}

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(App)